Author: Gao Pengbing (nbgao)
Email: nbgao@126.com
import numpy as np
import matplotlib.pyplot as plt
import colour
import warnings
%matplotlib inline
warnings.filterwarnings('ignore')
def ChannelShow(img):
img_r, img_g, img_b = [img[:,:,i] for i in range(3)]
plt.figure(figsize=(12, 6))
plt.subplot(221)
plt.imshow(img)
plt.title('Origin')
plt.axis('off')
plt.subplot(222)
plt.imshow(img_r, cmap='Reds_r')
plt.title('Red')
plt.axis('off')
plt.subplot(223)
plt.imshow(img_r, cmap='Greens_r')
plt.title('Green')
plt.axis('off')
plt.subplot(224)
plt.imshow(img_r, cmap='Blues_r')
plt.title('Blue')
plt.axis('off')
plt.tight_layout()
plt.show()
ChannelShow(img)
def MeanHistogram(img):
h, w = np.shape(img)[:2]
# 平均灰度图
img_mean = np.uint8(np.mean(img, axis=2))
# plt.figure(figsize=(4,3))
# plt.imshow(img_mean, cmap='gray')
# plt.axis('off')
# plt.show()
# 平均直方图
# img_mean = np.uint8(np.mean(img, axis=2))
# plt.figure(figsize=(8, 4))
# plt.hist(img_mean.ravel(), 256, [0, 255])
# plt.grid(ls=':')
# plt.show()
return img_mean
def HistogramEqualization(img):
N = 256
C = np.zeros(N)
h, w = np.shape(img)[:2]
for i in range(h):
for j in range(w):
C[img[i,j]] += 1
# 概率密度
C = C/np.sum(C)
# 累计概率密度
P = np.cumsum(C)
img_histeq = np.zeros_like(img)
for i in range(h):
for j in range(w):
img_histeq[i,j] = 255*P[img[i,j]]
img_histeq = np.uint8(img_histeq)
# 直方图
# plt.figure(figsize=(8, 4))
# plt.hist(img_histeq.ravel(), 256, [0, 255])
# plt.title('Equalization')
# plt.grid(ls=':')
# plt.show()
# plt.figure(figsize=(4,3))
# plt.imshow(img_histeq, cmap='gray')
# plt.axis('off')
# plt.show()
return img_histeq
for i in range(1,6):
file_path = 'Image/image'+str(i)+'.jpg'
img = plt.imread(file_path)
ChannelShow(img)
img_mean = MeanHistogram(img)
img_histeq = HistogramEqualization(img_mean)
plt.figure(figsize=(14,8))
plt.subplot(221)
plt.hist(img_mean.ravel(), 256, [0, 255])
plt.grid(ls=':')
plt.subplot(222)
plt.hist(img_histeq.ravel(), 256, [0, 255])
plt.grid(ls=':')
plt.subplot(223)
plt.imshow(img_mean, cmap='gray')
plt.title('Origin')
plt.axis('off')
plt.subplot(224)
plt.imshow(img_histeq, cmap='gray')
plt.title('Equalization')
plt.axis('off')
plt.show()
RGB空间转为HSI空间图像,对I(亮度,Intensity)通道进行直方图均衡化,再转为RGB图像
# RGB2HSI (single pixel)
def RGB2HSI(RGB):
# R,G,B \in [0,1]
RGB_norm = RGB / 255
[R, G, B] = RGB_norm
Max = max(RGB_norm)
Min = min(RGB_norm)
# Intensity
I = (Max + Min) / 2
# Satuation
S = 0.0
if(Max != Min):
S = (Max-I)/min(I,1-I)
# Hue
H = 0.0
if(Max==Min):
H = 0
elif(R == Max):
H = 0 + (G-B)/(Max-Min)/6
elif(G == Max):
H = 1/3 + (B-R)/(Max-Min)/6
elif(B == Max):
H = 2/3 + (R-G)/(Max-Min)/6
if(H<0):
H += 1
return [H, S, I]
# rgb2hsi
def rgb2hsi(img):
h, w = np.shape(img)[:2]
I_new = np.zeros([h, w, 3])
for i in range(h):
for j in range(w):
I_new[i,j,:] = RGB2HSI(img[i,j,:])
return I_new
# hsi2rgb
def hsi2rgb(img):
h, w = np.shape(img)[:2]
I_new = np.zeros([h, w, 3])
for i in range(h):
for j in range(w):
I_new[i,j,:] = colour.hsl2rgb(img[i,j,:])
return I_new
# rgb2hsi
img_hsi = rgb2hsi(img)
# Idensity
img_i = np.rint(img_hsi[:,:,2]*255)
img_i = img_i.astype('uint8')
# Idensity Histogram Equalization
img_i_eq = HistogramEqualization (img_i)
# HSI new
img_hsi_eq = img_hsi.copy()
img_hsi_eq[:,:,2] = img_i_eq / 255
# RGB new
img_rgb = hsi2rgb(img_hsi_eq) * 255
img_rgb = img_rgb.astype('uint8')
# RGB -> HSI
plt.figure(figsize=(16,8))
plt.subplot(221)
plt.imshow(img)
plt.title('RGB')
plt.axis('off')
plt.subplot(222)
plt.imshow(img_rgb)
plt.title('RGB Indensity Histogram Equalization')
plt.axis('off')
plt.subplot(223)
plt.hist(img_i.ravel(), 256, [0,255])
plt.title('Indensity Histogram')
plt.subplot(224)
plt.hist(img_i_eq.ravel(), 256, [0,255])
plt.title('Indensity Histogram Equalization')
plt.show()
# RGB -> HSI
for i in range(1,6):
file_path = 'Image/image'+str(i)+'.jpg'
img = plt.imread(file_path)
# rgb2hsi
img_hsi = rgb2hsi(img)
# Idensity
img_i = np.rint(img_hsi[:,:,2]*255)
img_i = img_i.astype('uint8')
# Idensity Histogram Equalization
img_i_eq = HistogramEqualization (img_i)
# HSI new
img_hsi_eq = img_hsi.copy()
img_hsi_eq[:,:,2] = img_i_eq / 255
# RGB new
img_rgb = hsi2rgb(img_hsi_eq) * 255
img_rgb = img_rgb.astype('uint8')
plt.figure(figsize=(16,8))
plt.subplot(221)
plt.imshow(img)
plt.title('RGB')
plt.axis('off')
plt.subplot(222)
plt.imshow(img_rgb)
plt.title('RGB Indensity Histogram Equalization')
plt.axis('off')
plt.subplot(223)
plt.hist(img_i.ravel(), 256, [0,255])
plt.title('Indensity Histogram')
plt.subplot(224)
plt.hist(img_i_eq.ravel(), 256, [0,255])
plt.title('Indensity Histogram Equalization')
plt.show()